package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.WXConfigBean;
import com.cskaoyan.bean.bo.ConfigMallBo;
import com.cskaoyan.bean.bo.ExpressConfigBo;
import com.cskaoyan.bean.bo.OrderConfigBo;
import com.cskaoyan.bean.po.ExpressConfigPo;
import com.cskaoyan.bean.vo.ConfigMallVo;
import com.cskaoyan.bean.vo.ExpressConfigVo;
import com.cskaoyan.bean.vo.OrderConfigVo;
import com.cskaoyan.service.ConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;

/**
 * @author: DYQ
 * @time: Created on 2022/6/5 14:27
 */

@RestController
@RequestMapping("admin/config")
public class ConfigController {

    @Autowired
    ConfigService configService;


    @GetMapping("mall")
    public BaseRespVo mall() {
        ConfigMallVo configMallVo = configService.queryMallConfig();
        return BaseRespVo.ok(configMallVo);
    }

    @PostMapping("mall")
    public BaseRespVo updateMallInfo(@RequestBody ConfigMallBo configMallBo) {
        if (!(StringUtils.isNumeric(configMallBo.toStringNumeric())&&
                configMallBo.getMarket_mall_latitude().matches(
                "^[\\-\\+]?(0(\\.\\d{1,8})?|([1-9](\\d)?)(\\.\\d{1,8})?|1[0-7]\\d{1}(\\.\\d{1,8})?|180(([.][0]{1,5})" +
                        "?))$")&&
                configMallBo.getMarket_mall_longitude().matches("^-?(([1-8]\\d?(\\.\\d{1,5})?)|90)$"))){
            return BaseRespVo.err("商品参数格式错误,请重新输入!", 200);
        }
        configService.updateMallConfigInfo(configMallBo);
        return BaseRespVo.ok(null);
    }

    @GetMapping("express")
    public BaseRespVo express() {
        ExpressConfigVo expressConfigVo = configService.queryExpress();
        return BaseRespVo.ok(expressConfigVo);
    }

    @PostMapping("express")
    public BaseRespVo updateExpressInfo(@RequestBody ExpressConfigBo expressConfigBo) {
        if (!StringUtils.isNumeric(expressConfigBo.toStringNumeric())){
            return BaseRespVo.err("商品参数格式错误,请重新输入!", 200);
        }
        configService.updateExpressConfig(expressConfigBo);
        return BaseRespVo.ok(null);
    }

    @GetMapping("order")
    public BaseRespVo order() {
        OrderConfigVo orderConfigVo = configService.queryOrderConfig();
        return BaseRespVo.ok(orderConfigVo);
    }

    @PostMapping("order")
    public BaseRespVo updateOrderInfo(@RequestBody OrderConfigBo orderConfigBo) {
        if (!StringUtils.isNumeric(orderConfigBo.toStringNumeric())){
            return BaseRespVo.err("商品参数格式错误,请重新输入!", 200);
        }
        configService.updateOrderConfig(orderConfigBo);
        return BaseRespVo.ok(null);
    }

    @GetMapping("wx")
    public BaseRespVo wx() {
        WXConfigBean wxConfigBean = configService.queryWXConfig();
        return BaseRespVo.ok(wxConfigBean);
    }

    @PostMapping("wx")
    public BaseRespVo updateWXConfig(@RequestBody WXConfigBean wxConfigBean)  {
        if (!StringUtils.isNumeric(wxConfigBean.toStringNumeric())){
            System.out.println(wxConfigBean.toStringNumeric());
            return BaseRespVo.err("商品参数格式错误,请重新输入!", 200);
        }
        configService.updateWXConfig(wxConfigBean);
        return BaseRespVo.ok(null);
    }

}
